const adModel = require("../model/adModel.js")
class Ad {
	// 获取广告列表
	static async geAd(ctx) {
		let {
			nowPage,
			pageSize
		} = ctx.request.query
		if (!nowPage) {
			nowPage = 1
		}
		if (!pageSize) {
			pageSize = 10
		}
		let res = await adModel.getAdList(nowPage, pageSize)
		ctx.body = {
			code: 0,
			codeInfo: "成功",
			data: res
		}
	}
	// 获取广告位置
	static async geAdPosition(ctx) {
		let {
			nowPage,
			pageSize
		} = ctx.request.query
		if (!nowPage) {
			nowPage = 1
		}
		if (!pageSize) {
			pageSize = 10
		}
		let res = await adModel.getAdPositionList(nowPage, pageSize)
		ctx.body = {
			code: 0,
			codeInfo: "成功",
			data: res
		}
	}

	// 添加广告
	static async addAd(ctx) {
		let {
			name,
			path,
			pid
		} = ctx.request.body
		if (!name && !path && !pid) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			let res = await adModel.addAd(name, path, pid)
			ctx.body = {
				code: 0,
				codeInfo: "成功",
				data:res
			}
		}

	}

	// 添加广告
	static async addAdPosition(ctx) {
		let {
			pname,
			keyword,
			status
		} = ctx.request.body
		if (!keyword) {
			keyword = ""
		}
		if (!pname && !status) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await adModel.addAdPosition(pname, keyword, status)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}

	}

	// 修改广告

	static async editAd(ctx) {
		let {
			name,
			path,
			pid,
			id
		} = ctx.request.body
		if (!name && !path && !pid && !id) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await adModel.editAd(id, name, path, pid)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}
	}

	// 修改广告位

	static async editAdPosition(ctx) {
		let {
			pname,
			keyword,
			status,
			id
		} = ctx.request.body

		if (!pname && !status && !id) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await adModel.editAdPosition(id, pname, keyword, status)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}
	}

	// 删除 广告
	static async delAd(ctx) {
		let {
			id
		} = ctx.request.query
		if (!id) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			await adModel.delAd(id)
			ctx.body = {
				code: 0,
				codeInfo: "成功"
			}
		}
	}

	// 删除 广告位
	static async delAdPosition(ctx) {
		let {
			id
		} = ctx.request.query
		if (!id) {
			ctx.body = {
				code: 1,
				codeInfo: "参数错误"
			}
		} else {
			let res = await adModel.delAdPosition(id)
			if (res) {
				ctx.body = {
					code: 0,
					codeInfo: "成功"
				}
			} else {
				ctx.body = {
					code: 2,
					codeInfo: "该广告位下有广告，不能删除！"
				}
			}

		}
	}


}

module.exports = Ad